package com.abel.bigwater.dataserver.model.ewide

import com.abel.bigwater.data.BcdString
import io.netty.buffer.Unpooled
import org.junit.Assert.*
import org.junit.Test

class TermCodeTypeTest {

    @Test
    fun parseBuf() {
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray("10 00 00 00 03"))
        val code = TermCodeType()
        assertTrue(code.parseBuf(buf))
        assertEquals("03", code.manuId?.reversedBcdString())
        assertEquals("00000010", code.codeBcd?.reversedBcdString())
        assertEquals("0300000010", code.fmt)
    }

    @Test
    fun pack() {
        val code = TermCodeType().apply {
            manuId = BcdString.fromBcdString("03")
            codeBcd = BcdString.fromBcdString("10000000", false)
        }
        assertEquals("0300000010", code.fmt)

        val buf = Unpooled.buffer()
        code.pack(buf)
        assertArrayEquals(byteArrayOf(0x10, 0, 0, 0, 0x03), ByteArray(5).apply { buf.readBytes(this) })
    }
}